<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="__layuimini__/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="__layuimini__/css/public.css" media="all">
    <style>
        .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
            height: 34px;
            line-height: 34px;
            padding: 0 8px;
        }
        /*.layui-table-cell{*/
        /*display:table-cell;*/
        /*vertical-align: middle;*/
        /*}*/
        able{table-layout: fixed;}
        td{word-break: break-all; word-wrap:break-word;}
        .layui-fluid {
            height:auto;
        }
        .layui-table-cell  {
            height:auto;
            overflow:visible;
            text-overflow:inherit;
            white-space:pre-line;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form id="myform" class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">模型名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">表名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="table" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">引擎</label>
                            <div class="layui-input-inline">
                                <select name="engine" class="layui-select" lay-search>
                                    <option value="">请选择</option>
                                    <option value="InnoDB">InnoDB</option>
                                    <option value="MyISAM">MyISAM</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">表格类型</label>
                            <div class="layui-input-inline">
                                <select name="tabletype" class="layui-select" lay-search>
                                    <option value="">请选择</option>
                                    <option value="ordinary">普通表格</option>
                                    <option value="tree">树状表格</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="button" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" data-retype="button" data-action="{:url('system.models/updatetable')}" lay-event="update">更新表</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 全部删除 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" data-retype="a" data-action="{:url('system.field/index')}?field=table_id" lay-event="ab">字段</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
            {if $adminuser['id']==1}
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" data-retype="button" data-action="{:url('system.models/clineartable')}" lay-event="ab">清除数据</a>
            {/if}
            <!--<a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" data-retype="button" data-action="{:url('system.models/ces')}" lay-event="ab">api</a>-->
        </script>
        <script type="text/html" id="is_page">
            {{#  if(d.is_page==1){ }}
            <input type="checkbox" field="is_page" value="{{d.id}}" lay-skin="switch" lay-text="开启|关闭" lay-filter="sexDemo" checked>
            {{#  } else { }}
            <input type="checkbox" field="is_page" value="{{d.id}}" lay-skin="switch" lay-text="开启|关闭" lay-filter="sexDemo">
            {{#  } }}
        </script>

    </div>
</div>
<script src="__layuimini__/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        table.render({
            elem: '#currentTableId',
            url: '{:url("system.models/index")}',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id',  title: 'ID', sort: true,width:50,align: "center"},
                {field: 'name', title: '模型名',align: "center"},
                {field: 'table',  title: '表名', sort: true,align: "center"},
                {field: 'engine',  title: '引擎',align: "center"},
                {field: 'tabletype', title: '表格类型',align: "center"},
                {field: 'is_page', title: '开启分页',templet:"#is_page",align: "center"},
                {field: 'create_time',  title: '创建时间', align: "center"},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 30, 50, 100, 500, 1000,5000,10000,20000],
            limit: 10,
            page: true,
            // totalRow:'0',
            done:function () {
                // layer.msg('加载完成')
            }
            // skin: 'line'
        });
        function table_reload(){
            // console.log();
            var text = $(".layui-laypage-curr").children('.layui-laypage-em').next().text();
            var field = $("#myform").serializeArray();
            // var mytiaojian = '';
            var objdata = {};
            $.each(field,function (i,v) {
                if(v.value!==''){
                    // mytiaojian += v.name+'='+v.value+"&";
                    objdata[v.name] = v.value;
                }
            })
            table.reload('currentTableId', {
                page: {
                    curr: text
                }
                , where: {
                    searchParams: JSON.stringify(objdata)
                }
            }, 'data');
            // mytiaojian = mytiaojian.substr(0,mytiaojian.length-1);
        }
        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            console.log(data.field);
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');

            return false;
        });
        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            var field = $("#myform").serializeArray();
            var mytiaojian = '';
            var objdata = {};
            $.each(field,function (i,v) {
                if(v.value!==''){
                    mytiaojian += v.name+'='+v.value+"&";
                    objdata[v.name] = v.value;
                }
            })
            mytiaojian = mytiaojian.substr(0,mytiaojian.length-1);
            var action = $(this).attr('data-action');
            var type = $(this).attr('data-retype');
            if(mytiaojian!=''){
                if(action.indexOf('?')!= -1){
                    action = action+'&'+mytiaojian;
                }else{
                    action = action+"?"+mytiaojian;
                }
            }
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['95%', '95%'],
                    content: '{:url("system.models/add")}',
                    end:function () {
                        table_reload();
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);

                });
                return false;
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                var id = [];
                $.each(data,function (i,v) {
                    if(v.id){
                        id.push(v.id);
                    }
                })
                // id  = id.substr(0,id.length-1);
                console.log(id)
                // table_reload();
                var index = layer.load();
                $.post("{:url('system.models/delete')}",{id:id},function (res) {
                    console.log(res);
                    layer.close(index);
                    if(res.code==200){
                        layer.msg(res.msg,function () {
                            table_reload();
                        });
                    }else{
                        layer.msg(res.msg);
                    }
                })
                return false;
            }else if(obj.event === 'update'){
                console.log(field);
                console.log(action);
                if(type=='a'){
                    var index = layer.open({
                        title: '添加',
                        type: 2,
                        shade: 0.2,
                        maxmin:true,
                        shadeClose: true,
                        area: ['95%', '95%'],
                        content: action,
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                }else if(type=='button'){
                    var index = layer.load();
                    $.post(action,objdata,function (res) {
                        layer.close(index);
                        if(res.code==200){
                            layer.msg(res.msg);
                        }else{
                            layer.msg(res.msg);
                        }
                    })
                }
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var url = '{:url("system.models/edit")}';
                if(url.indexOf('?')!= -1){
                    url = url+'&id='+data.id;
                }else{
                    url = url+"?id="+data.id;
                }
                var index = layer.open({
                    title: '编辑',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['95%', '95%'],
                    content: url,
                    end:function () {
                        table_reload();
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                var index = layer.load();
                $.post("{:url('system.models/delete')}",{id:data.id},function (res) {
                    console.log(res)
                    layer.close(index);
                    if(res.code==200){
                        layer.msg(res.msg)
                        obj.del();
                    }else{
                        layer.msg(res.msg);
                    }
                });
            }else if(obj.event === 'ab'){
                var url = $(this).attr('data-action');
                if(url.indexOf('?')!= -1){
                    url = url+'&id='+data.id;
                }else{
                    url = url+"?id="+data.id;
                }
                var type = $(this).attr('data-retype');
                if(type=='a'){
                    var index = layer.open({
                        title: '',
                        type: 2,
                        shade: 0.2,
                        maxmin:true,
                        shadeClose: true,
                        area: ['95%', '95%'],
                        content: url,
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    return false;
                }else if(type=='button'){
                    var index = layer.load();
                    $.post(url,{},function (res) {
                        console.log(res);
                        layer.close(index)
                        if(res.code==200){
                            layer.msg(res.msg);
                        }else{
                            layer.msg(res.msg);
                        }
                    })
                    return false;
                }
            }
        });
        /**
         * 监听开关
         * */
        form.on('switch(sexDemo)',function (data) {
            console.log(data);
            var status = data.elem.checked;
            // console.log(status);
            var id = data.value;
            var field = $(this).attr('field');
            var index = layer.load();
            $.post('{:url("system.models/modify")}',{id:id,field:field},function (res) {
                layer.close(index);
                console.log(res);
                if(res.code==200){
                    layui.layer.msg(res.msg);
                }else{
                    layui.layer.msg(res.msg);
                    // table_reload();
                    data.elem.checked = !status;
                }
            })
        })

    });
</script>

</body>
</html>